const mongoose = require('mongoose');

const settingSchema = new mongoose.Schema({
  // 基本设置
  siteName: {
    type: String,
    required: [true, '网站名称不能为空'],
    trim: true,
    maxlength: [50, '网站名称不能超过50个字符']
  },
  siteDescription: {
    type: String,
    trim: true,
    maxlength: [200, '网站描述不能超过200个字符']
  },
  icpNumber: {
    type: String,
    trim: true,
    maxlength: [30, 'ICP备案号不能超过30个字符']
  },

  // 上传设置
  maxVideoSize: {
    type: Number,
    default: 500,
    min: [1, '视频大小限制不能小于1MB'],
    max: [10000, '视频大小限制不能超过10000MB']
  },
  allowedVideoFormats: {
    type: [String],
    default: ['mp4', 'webm'],
    validate: {
      validator: function(formats) {
        const validFormats = ['mp4', 'webm', 'avi', 'mov', 'flv'];
        return formats.every(format => validFormats.includes(format));
      },
      message: '不支持的视频格式'
    }
  },

  // 评论设置
  enableComments: {
    type: Boolean,
    default: true
  },
  commentNeedsReview: {
    type: Boolean,
    default: true
  },
  commentInterval: {
    type: Number,
    default: 30,
    min: [0, '评论间隔不能小于0秒'],
    max: [3600, '评论间隔不能超过3600秒']
  },

  // 用户设置
  enableRegistration: {
    type: Boolean,
    default: true
  },
  defaultUserRole: {
    type: String,
    default: 'user',
    enum: ['user', 'admin']
  },
  defaultUserStatus: {
    type: String,
    default: 'active',
    enum: ['active', 'inactive']
  },

  // 视频设置
  videoNeedsReview: {
    type: Boolean,
    default: true
  },
  defaultVideoStatus: {
    type: String,
    default: 'pending',
    enum: ['pending', 'published', 'rejected']
  },
  enableVideoDownload: {
    type: Boolean,
    default: false
  },

  // 存储设置
  storageType: {
    type: String,
    default: 'local',
    enum: ['local', 's3']
  },
  storageConfig: {
    type: Map,
    of: String,
    default: new Map()
  },

  // 邮件设置
  emailConfig: {
    host: String,
    port: Number,
    secure: Boolean,
    user: String,
    pass: String,
    from: String
  },

  // 缓存设置
  enableCache: {
    type: Boolean,
    default: true
  },
  cacheExpiration: {
    type: Number,
    default: 3600,
    min: [60, '缓存过期时间不能小于60秒']
  },

  // 备份设置
  autoBackup: {
    type: Boolean,
    default: false
  },
  backupInterval: {
    type: Number,
    default: 24 * 60 * 60, // 24小时
    min: [3600, '备份间隔不能小于1小时']
  },
  maxBackupFiles: {
    type: Number,
    default: 10,
    min: [1, '至少保留1个备份文件'],
    max: [100, '最多保留100个备份文件']
  },

  // 系统信息
  lastBackupTime: Date,
  lastCacheClearTime: Date,
  systemVersion: String,
  updatedAt: {
    type: Date,
    default: Date.now
  }
}, {
  timestamps: true
});

// 更新时自动更新 updatedAt 字段
settingSchema.pre('save', function(next) {
  this.updatedAt = new Date();
  next();
});

// 确保只有一条设置记录
settingSchema.statics.findOneOrCreate = async function() {
  const settings = await this.findOne();
  if (settings) {
    return settings;
  }
  return this.create({
    siteName: '我的个人网站', // 提供默认值
    siteDescription: '这是一个视频分享平台'
    // 其他字段使用模型中的默认值
  });
};

module.exports = mongoose.model('Setting', settingSchema);